﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Safe_Driving.TokenUser;
using DbLinq.Data.Linq;

namespace Safe_Driving
{
    /// <summary>
    /// Logique d'interaction pour Connection.xaml
    /// </summary>
    public partial class Connection : Window
    {
        public Connection()
        {

            InitializeComponent();

            AppSafeDriving appDB = SingletonTokenUser.GetInstance().DbApp;

            IEnumerable<string> list_agence = (from agences in appDB.SFAgeNcE
                              select agences.NoM);

            comboBoxAgence.ItemsSource = list_agence;
        }

        

        private void home_signinButton_Click(object sender, RoutedEventArgs e)
        {
            SingletonTokenUser token = SingletonTokenUser.GetInstance();
            AppSafeDriving app = (AppSafeDriving)token.DbApp;

            string agence_name = (string) comboBoxAgence.SelectedItem;

            token.Agence= (from ag in app.SFAgeNcE
                               where ag.NoM == agence_name
                               select ag).SingleOrDefault();
           
            Security s = new Security();
            string loginEntre = connection_login_textbox.Text;
            string passwordEntre = s.EncodePassword(connection_password_textbox.Password);

            //Vérifie si les identiants entrés sont corrects.
            //Les pass étant cryptés en base, on compare les hashs des pass.
            try
            {
                SFCommercial comm = (from c in app.SFCommercial
                                     where c.Login == loginEntre && c.Password == passwordEntre
                                     select c).SingleOrDefault();

                if (comm != null)
                {
                    if (!(comm.Login == loginEntre && comm.Password == s.EncodePassword(connection_password_textbox.Password)))
                        MessageBox.Show("Erreur d'authentification", "Safe Driving", MessageBoxButton.OK, MessageBoxImage.Stop);
                    else
                    {
                        token.User_type = SingletonTokenUser.commercial;
                        token.Commercial_connected = comm;
                        MainWindow window = new MainWindow();
                        token.Mw = window;
                        window.Show();
                        this.Close();
                    }
                }
                else
                {
                    SFMonItEUR mono = (from m in app.SFMonItEUR
                                       where m.Login == loginEntre && m.Password == passwordEntre
                                       select m).SingleOrDefault();
                    if (mono != null)
                    {
                        if (!(mono.Login == loginEntre && mono.Password == s.EncodePassword(connection_password_textbox.Password)))
                            MessageBox.Show("Erreur d'authentification", "Safe Driving", MessageBoxButton.OK, MessageBoxImage.Stop);
                        else
                        {
                            token.User_type = SingletonTokenUser.moniteur;
                            token.Moniteur_connected = mono;
                            MainWindow window = new MainWindow();
                            token.Mw = window;
                            window.Show();
                            this.Close();
                        }
                    }
                    else
                        MessageBox.Show("N'existe pas dans commercial ET dans moniteur");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("POUR LE DEBUG :\n" + ex.Message);
            }
        }

        private void button_Moniteur_Click(object sender, RoutedEventArgs e)
        {
            Security s = new Security();
            
            string passwordEntre = s.EncodePassword("onche");
            SingletonTokenUser token = SingletonTokenUser.GetInstance();
            SingletonTokenUser.GetInstance().User_type = SingletonTokenUser.moniteur;
            AppSafeDriving app = (AppSafeDriving)token.DbApp;
            SFMonItEUR mono = (from m in app.SFMonItEUR
                               where m.Login == "Moniteur" && m.Password == passwordEntre
                               select m).SingleOrDefault();
            SingletonTokenUser.GetInstance().Moniteur_connected = mono;

            MainWindow window = new MainWindow();

            window.Show();

            this.Close();
        }

        private void test_fields()
        {
            if (connection_login_textbox.Text == "" || connection_password_textbox.Password == "")
            {
                home_signinButton.IsEnabled = false;
            }
            else
            {
                home_signinButton.IsEnabled = true;

            }
        }

        private void connection_login_textbox_TextChanged(object sender, TextChangedEventArgs e)
        {
            test_fields();
        }

        private void connection_password_textbox_PasswordChanged(object sender, RoutedEventArgs e)
        {
            test_fields();
        }

    }
}
